<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Model\Room;
use App\Model\Main;
use App\Model\Member;
use Closure;
use JWTAuth;

class RoomApiController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xApi' => 'am-in',
        ]);
    }


    function getRoomList()
    {

        $data = array();
        $room = Room::select('room')->get();

        if ($room->isEmpty()) {
            $data['info'] = '获取数据失败';
            $data['status'] = false;
            return $data;
        }

        $data['status'] = true;
        $data['info'] = '成功获取信息';
        foreach ($room as $key => $value) {
            $data['room'][] = $value['room'];
        }

        $data['token'] = $this->userAuth();
        return $data;
    }


    function getDeviceList(Request $request)
    {
        $data = array();
        if (!$request->device && !$request->room) {
            $data['info'] = '请输入请求参数';
            $data['status'] = false;
            return view('Admin.xHotel.api.getDeviceList')->with(['data' => $data, '_api_device' => 'am-active']);
        }

        if (!empty($request->all())) {

            $room = Room::where('room', $request->room)->first();

            $room->device = $request->device;
            $room->save();

            if (!$room->save()) {
                $data['info'] = '添加数据失败';
                $data['status'] = false;
                return $data;
            }

            $data['status'] = true;
            $data['info'] = '添加数据成功';
            return view('Admin.xHotel.api.getDeviceList')->with(['data' => $data, '_api_device' => 'am-active']);
        }

    }

    public function getRoom(Request $request)
    {

        $data = array();
        if (!$request->room) {
            $data['info'] = '请输入请求参数';
            $data['status'] = false;
            return view('Admin.xHotel.api.getRoom')->with(['data' => $data, '_api_username' => 'am-active']);
        }

        $main = Main::with(['main_galleries' => function ($query) {
            $query->select('main_id', 'img');
        }])->get();

        foreach ($main->toArray() as $key => $value) {
            if ($value['thumb'] == "") {
                $result['thumb'] = 'back_img.jpg';
            } else {
                $result['thumb'] = $value['thumb'];
            }

            if (empty($value['main_galleries'])) {
                $result['gallery'][] = 'back_img.jpg';
            }
            {
                foreach ($value['main_galleries'] as $k => $v) {
                    $result['gallery'][] = $v['img'];
                }
            }
            $result['salutatory'] = $value['salutatory'];
            $result['en_salutatory'] = $value['en_salutatory'];
        }


        $rooms = Room::where('room', $request->room)->get()->toArray();
        if (empty($rooms)) {
            $data['info'] = '获取数据失败';
            $data['status'] = false;
            $result['username'] = '欢迎入住';
            $result['sex'] = 0;
            $data['data'] = $result;
            return $data;
        }

        $room = Room::where('room', $request->room)->select('id')->first()->toArray();

        $username = Member::with(['room' => function ($query) {
            $query->select('id', 'room', 'device');
        }])->where('room_id', $room)->select('room_id', 'username', 'sex')->get();

        //return $username;
        $result = array();
        if ($username->isEmpty()) {
            $result['username'] = "欢迎入住";
            $result['sex'] = 1;
            if (empty($main->main_galleries)) {
                $result['gallery'][] = 'back_img.jpg';
            }
        }

        $data['status'] = true;
        $data['info'] = '成功获取信息';
        foreach ($username->toArray() as $v) {
            $result['username'] = $v['username'];
            $result['sex'] = $v['sex'];
        }

        foreach ($main->toArray() as $key => $value) {
            if ($value['thumb'] == "") {
                $result['thumb'] = 'back_img.jpg';
            } else {
                $result['thumb'] = $value['thumb'];
            }

            if (empty($value['main_galleries'])) {
                $result['gallery'][] = 'back_img.jpg';
            }
            {
                foreach ($value['main_galleries'] as $k => $v) {
                    $result['gallery'][] = $v['img'];
                }
            }

            $result['salutatory'] = $value['salutatory'];
            $result['en_salutatory'] = $value['en_salutatory'];
        }

        $data['data'] = $result;
        return $data;

    }


}
